package com.example.megaandroidrssreader;

import android.app.Activity;
import android.app.DialogFragment;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SourceEditorFragment extends DialogFragment {

	public static final String DIALOG_IS_ADDING_NEW = "Dialog.IsAddingNew";
	public static final String DIALOG_RSS_SOURCE_INDEX = "Dialog.RssSourceIndex";
	
	protected OnRssSourceModifyListener mActivity;
	
	public static SourceEditorFragment newInstance(boolean isAddingNew, RssSource rssSource, int rssSourceIndex) {
		SourceEditorFragment fragment = new SourceEditorFragment();

		Bundle bundle = new Bundle();
		if (isAddingNew) {
			bundle.putString(RssSource.RSS_SOURCE_TITLE, "");
			bundle.putString(RssSource.RSS_SOURCE_URL, "");
			bundle.putInt(DIALOG_RSS_SOURCE_INDEX, -1);
		} else {
			bundle.putString(RssSource.RSS_SOURCE_TITLE, rssSource.mTitle);
			bundle.putString(RssSource.RSS_SOURCE_URL, rssSource.mUrl);
			bundle.putByteArray(RssSource.RSS_SOURCE_ICON, RssReaderDaoImpl.bitmapToByteArray(rssSource.mFavicon));
			bundle.putInt(DIALOG_RSS_SOURCE_INDEX, rssSourceIndex);
		}
		bundle.putBoolean(DIALOG_IS_ADDING_NEW, isAddingNew);
		fragment.setArguments(bundle);
		
		return fragment;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof OnRssSourceModifyListener) {
			mActivity = (OnRssSourceModifyListener) activity;
		} else {
			Toast.makeText(getActivity(), "Dialog called from Activity that does not implement OnRssSourceModifyListener", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View dialogView = inflater.inflate(R.layout.fragment_source_edit, container, false);
		final EditText edtRssSourceTitle = (EditText) dialogView.findViewById(R.id.edtRssSourceTitle);
		final EditText edtRssSourceUrl = (EditText) dialogView.findViewById(R.id.edtRssSourceUrl);
		
		if (getArguments().getBoolean(DIALOG_IS_ADDING_NEW)) {
			getDialog().setTitle("Adding new RSS source...");
			edtRssSourceTitle.setText("");
			edtRssSourceUrl.setText("");
		} else {
			getDialog().setTitle("Modifying RSS source...");
			edtRssSourceTitle.setText(getArguments().getString(RssSource.RSS_SOURCE_TITLE));
			edtRssSourceUrl.setText(getArguments().getString(RssSource.RSS_SOURCE_URL));
		}
		
		Button btnOkSourceEditFragment = (Button) dialogView.findViewById(R.id.btnOkSourceEditFragment);
		btnOkSourceEditFragment.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				RssSource rssSource = new RssSource();
				rssSource.setTitle(edtRssSourceTitle.getText().toString());
				rssSource.setRssUrl(edtRssSourceUrl.getText().toString());
				byte[] favicon = getArguments().getByteArray(RssSource.RSS_SOURCE_ICON);
				if (favicon != null) {
					rssSource.mFavicon = BitmapFactory.decodeByteArray(favicon, 0, favicon.length);
				} else {
					rssSource.mFavicon = null;
				}
				mActivity.onRssSourceModify(rssSource,
						getArguments().getInt(DIALOG_RSS_SOURCE_INDEX),
						getArguments().getBoolean(DIALOG_IS_ADDING_NEW));
				dismiss();
			}
			
		});
		Button btnCancelSourceEditFragment = (Button) dialogView.findViewById(R.id.btnCancelSourceEditFragment);
		btnCancelSourceEditFragment.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});

		return dialogView;
	}

}
